package com.nineclock.doc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.doc.dto.DocFileDto;
import com.nineclock.doc.dto.DocFolderDto;
import com.nineclock.doc.mapper.DocFileDtoMapper;
import com.nineclock.doc.mapper.DocFolderDtoMapper;
import com.nineclock.doc.pojo.DocFile;
import com.nineclock.doc.pojo.DocFolder;
import com.nineclock.doc.service.DocFileDtoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DocFileDtoServiceImpl implements DocFileDtoService {
    @Autowired
    DocFileDtoMapper docFileDtoMapper;

    @Override
    public void addDopController(DocFileDto docFileDto) {
        if (docFileDto == null) {
            throw new NcException(ResponseEnum.FILE_WRITER_ERROR);
        }

        Long companyId = CurrentUserHolder.get()
                .getCompanyId();
        Long companyUserId = CurrentUserHolder.get().getCompanyUserId();
        DocFile docFile = BeanHelper.copyProperties(docFileDto, DocFile.class);
        //docFileDtoMapper.insert();
        docFile.setStatus((short) 1);
        docFile.setPermission((short) 1);
        docFile.setType((short) 1);
        docFile.setCompanyId(companyId);

        docFile.setCreateUserId(companyUserId);
        docFileDtoMapper.insert(docFile);


    }

    //查询所有的文件
    @Override
    public List<DocFileDto> queryCompanyMemberList() {

        //获取企业id
        Long companyId = CurrentUserHolder.get().getCompanyId();
        //查询当前企业的员工列表
        LambdaQueryWrapper<DocFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocFile::getCompanyId, companyId);
        List<DocFile> docFileList = docFileDtoMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(docFileList)) {
            List<DocFileDto> docFileDtoList = BeanHelper.copyWithCollection(docFileList, DocFileDto.class);
            return docFileDtoList;
        }


        return null;
    }

    @Autowired
    DocFolderDtoMapper docFolderDtoMapper;

    @Override
    public List<DocFolderDto> queryListDis() {
        //获取企业id
        Long companyId = CurrentUserHolder.get().getCompanyId();
        //查询当前企业的员工列表
        LambdaQueryWrapper<DocFolder> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(DocFolder::getCompanyId, companyId);
        List<DocFolder> docFolders = docFolderDtoMapper.selectList(wrapper2);
        if (CollectionUtil.isNotEmpty(docFolders)) {
            List<DocFolderDto> docFolderDtos = BeanHelper.copyWithCollection(docFolders, DocFolderDto.class);
            return docFolderDtos;

        }


        return null;
    }
}

